package com.google.code.tcx2nike.services;

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.ContentType.TEXT;
import static groovyx.net.http.ContentType.URLENC


public class GarminJSONservice {
	def HTTPBuilder http
	
	GarminJSONservice () {
		this.http = new HTTPBuilder()
	}
	
	
	public static void main(String[] args) {
		def gService =  new GarminJSONservice()
		//gService.test()
		gService.auth()
		gService.test()
	}
	
	def auth() {
		def loginUrl = 'http://connect.garmin.com'
		
		def http = new HTTPBuilder( loginUrl )
		def postBody = ['login:loginUsernameField':'jansch','login:password':'holly123'] // will be url-encoded
		
		http.post( path: '/signin', body: postBody, 
				requestContentType: URLENC ) { resp ->
					
					println "Status: ${resp.status}"
					assert resp.statusLine.statusCode == 200
				}
		
		
		
	}
	
	def test() {
		
		http.request( 'http://connect.garmin.com', GET, TEXT ) { req ->
			uri.path = '/proxy/activity-search-service-1.0/json/activities'
			uri.query = [ dc:'1220170621856', start: '0', limit: '10' ]
			headers.'User-Agent' = "Mozilla/5.0 Firefox/3.0.4"
			
			response.success = { resp, stream ->
				assert resp.statusLine.statusCode == 200
				println "Got response: ${resp.statusLine}"
				//println "Content-Type: ${resp.headers.'Content-Type'"
				System.out << stream // print response stream
			}
			
			response.'404' = { println 'Not found' }
		}
		
		
		
	}
	
}